package com.pks.datacenter.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.pks.datacenter.interceptor.MyInterceptor;
import com.pks.util.UtilCollection;
import com.pks.util.freemarker.MyFreeMarkerConfigurer;
import com.pks.web.springmvc.interceptor.SystemContextInterceptor;

@Configuration
// @EnableWebMvc+WebMvcConfigurationAdapter 可重写父类的方法；屏蔽@EnableAutoConfiguration
// WebMvcConfigurationSupport 可重写父类的方法；屏蔽 @EnableAutoConfiguration<p>
// WebMvcConfigurationAdapter 可重写父类的方法；@EnableAutoConfiguration 有效<p>
public class WebConfig implements WebMvcConfigurer {

	@SuppressWarnings("deprecation")
	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		// 精确匹配URL的后缀
		configurer.setUseSuffixPatternMatch(false);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// http://localhost/upload/** --> D:/transin/files/upload/**
		registry.addResourceHandler("/" + MyConfig.FOLDER_UPLOAD + "/**").addResourceLocations(
				"file:" + MyConfig.getSavePath() + MyConfig.FOLDER_UPLOAD + "/");
		// http://localhost/download/** --> D:/transin/files/download/**
		registry.addResourceHandler("/" + MyConfig.FOLDER_DOWNLOAD + "/**").addResourceLocations(
				"file:" + MyConfig.getSavePath() + MyConfig.FOLDER_DOWNLOAD + "/");
		
	}

	// 不用写控制器，直接配置请求地址的转向
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("forward:/index/index.do");
		registry.addViewController(MyConfig.URL_LOGIN).setViewName("login");
	}
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(systemContextInterceptor());
		registry.addInterceptor(myInterceptor());
//		registry.addInterceptor(sValiditorInterceptor());
	}

	@Bean(name = "systemContextInterceptor")
	public SystemContextInterceptor systemContextInterceptor() {
		SystemContextInterceptor result = new SystemContextInterceptor();
		result.setPagesizeKey("limit,pagesize,pageSize");
		result.setOffsetKey("start,offset");
		result.setPageKey("page");
		result.setDefaultPagesize(10);
		result.setIframeCrossDomain(false);
		return result;
	}

	@Bean(name = "myInterceptor")
	public MyInterceptor myInterceptor() {
		MyInterceptor result = new MyInterceptor();
		result.setLoginUrl(MyConfig.URL_LOGIN);
		List<String> excludeUrls = new ArrayList<String>();
		// swagger2 相关所需要的开放的
		excludeUrls.addAll(UtilCollection.initArrayList("/v2/api-docs","/swagger-resources*.*","/webjars*.*","/swagger-ui.html"));
		// 登录接口
		excludeUrls.addAll(UtilCollection.initArrayList("/user/loginByToken","/user/login"));
		// 对外接口 进行开放   自定义路径开放到支持下面扩展到3级 ， 如需要增加 需要在apiController 中进行添加 
 		excludeUrls.addAll(UtilCollection.initArrayList("/api/getToken"));
 		excludeUrls.addAll(UtilCollection.initArrayList("/api(/[\\w\\_]+){1,3}"));
		result.setExcludeUrls(excludeUrls);
		return result;
	}
	
	@Bean
	public FreeMarkerConfigurer freeMarkerConfigurer() {
		MyFreeMarkerConfigurer freeMarkerConfigurer = new MyFreeMarkerConfigurer();
		freeMarkerConfigurer.setDefaultEncoding("UTF-8");
		freeMarkerConfigurer.setTemplateLoaderPath(UiConfig.TEMPLATE_LOADER_PATH);
		// freeMarkerConfigurer.setPreferFileSystemAccess(false);//ftl文件在其他Jar包里时需要此设置才能正确加载
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("contextPath", MyConfig.getContextPath());
		variables.put("urlLogin", MyConfig.URL_LOGIN);
		freeMarkerConfigurer.setFreemarkerVariables(variables);
		Properties settings = new Properties();
		settings.setProperty("template_update_delay", "0");// 模版更新时间
		settings.setProperty("defaultEncoding", "UTF-8");
		settings.setProperty("url_escaping_charset", "UTF-8");
		settings.setProperty("locale", "zh_CN");
		settings.setProperty("boolean_format", "true,false");
		settings.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
		settings.setProperty("date_format", "yyyy-MM-dd");
		settings.setProperty("time_format", "HH:mm:ss");
		settings.setProperty("number_format", "0.######");
		settings.setProperty("whitespace_stripping", "true"); // 去掉多余的空格
		settings.setProperty("classic_compatible", "true");// 变量为空时返回空串
		settings.setProperty("object_wrapper", "freemarker.ext.beans.BeansWrapper");// 使Bean更兼容Java
		settings.setProperty("auto_import", "tools/index.ftl as z");
		freeMarkerConfigurer.setFreemarkerSettings(settings);
		return freeMarkerConfigurer;
	}

}
